<?php
define('IN_SEXMALL', true);

require(dirname(__FILE__) . '/includes/init.php');
$exc   = new exchange(TABLEPRE."act_url", $db, 'act_id', 'act_name');

if ($_REQUEST['act'] == 'list')
{
	admin_priv('activity_url');

	$urls = get_act_url_list();

	$smarty->assign('urls',    		$urls['arr']);
	$smarty->assign('filter',       $urls['filter']);
	$smarty->assign('record_count', $urls['record_count']);
	$smarty->assign('page_count',   $urls['page_count']);

	$smarty->assign('ur_here',     '活动链接');
	$smarty->assign('action_link', array('text' => '添加活动链接', 'href' => 'act_url.php?act=add'));
	$smarty->assign('full_page',   1);

	$smarty->display('act_url_list.htm');
}

elseif ($_REQUEST['act'] == 'query')
{
	check_authz_json('activity_url');

	$urls = get_act_url_list();

	$smarty->assign('urls',    		$urls['arr']);
	$smarty->assign('filter',       $urls['filter']);
	$smarty->assign('record_count', $urls['record_count']);
	$smarty->assign('page_count',   $urls['page_count']);

	$sort_flag  = sort_flag($urls['filter']);
	$smarty->assign($sort_flag['tag'], $sort_flag['img']);

	make_json_result($smarty->fetch('act_url_list.htm'), '',
	array('filter' => $urls['filter'], 'page_count' => $urls['page_count']));
}

else if ($_REQUEST['act'] == 'add')
{
	admin_priv('activity_url');

	$smarty->assign('ur_here',       '添加活动链接');
	$smarty->assign('action_link',   array('href' => 'act_url.php?act=list', 'text' => '活动链接列表'));

	$smarty->assign('form_act', 'insert');
	$smarty->assign('action',   'add');

	//assign_query_info();
	$smarty->display('act_url_info.htm');
}

elseif ($_REQUEST['act'] == 'insert')
{
	admin_priv('activity_url');

	/* 初始化变量 */
	$act_name 		= !empty($_POST['act_name']) ? trim($_POST['act_name']) : '';
	$url 	= !empty($_POST['url']) ? trim($_POST['url']) : 0;

	/* 获得开始时期与结束日期 */
	$start_time = local_strtotime($_POST['start_time']);
	$stop_time   = local_strtotime($_POST['stop_time']);

	$data = array(
    	'act_name'		=> $act_name,
		'url'			=> $url,
    	'start_time'	=> $start_time,
    	'stop_time'		=> $stop_time,
    	'add_time'		=> local_time(),
	);

	$db->autoExecute(TABLEPRE."act_url", $data, 'INSERT');

	/* 记录管理员操作 */
	admin_log($act_name, 'add', '活动链接');

	clear_cache_files(); // 清除模版缓存

	$link[1]['text'] = '活动链接列表';
	$link[1]['href'] = 'act_url.php?act=list';

	$link[2]['text'] = '继续添加';
	$link[2]['href'] = 'act_url.php?act=add';

	sys_msg('添加' . "&nbsp;" .$act_name . "&nbsp;" . '成功',0, $link);

}

elseif ($_REQUEST['act'] == 'edit')
{
	admin_priv('activity_url');

	$act_id = !empty($_GET['act_id']) ? intval($_GET['act_id']) : 0;

	$sql = "SELECT act_id, act_name, url, start_time, stop_time, add_time".
		" FROM ".TABLEPRE."act_url WHERE act_id = '".$act_id."'";
	$urls = $db->getRow($sql);

	$urls['start_time'] = date("Y-m-d H:i:s", $urls['start_time']);
	$urls['stop_time'] = date("Y-m-d H:i:s", $urls['stop_time']);

	$smarty->assign('ur_here',       '活动链接编辑');
	$smarty->assign('action_link',   array('href' => 'act_url.php?act=list', 'text' => '活动链接列表'));
	$smarty->assign('form_act',      'update');
	$smarty->assign('action',        'edit');
	$smarty->assign('urls',      $urls);

	assign_query_info();
	$smarty->display('act_url_info.htm');
}

elseif ($_REQUEST['act'] == 'update')
{
	admin_priv('activity_url');

	/* 初始化变量 */
	$act_id = !empty($_POST['act_id']) ? intval($_POST['act_id']) : 0;
	$act_name 		= !empty($_POST['act_name']) ? trim($_POST['act_name']) : '';
	$url 	= !empty($_POST['url']) ? trim($_POST['url']) : 0;

	/* 获得开始时期与结束日期 */
	$start_time = strtotime($_POST['start_time']);
	$stop_time   = strtotime($_POST['stop_time']);

	$data = array(
    	'act_name'		=> $act_name,
		'url'			=> $url,
    	'start_time'	=> $start_time,
    	'stop_time'		=> $stop_time,
    	'add_time'		=> local_time(),
	);

	$db->autoExecute(TABLEPRE."act_url", $data, 'UPDATE', 'act_id='.$act_id);

	/* 记录管理员操作 */
	admin_log($act_name, 'edit', '活动链接');

	clear_cache_files(); // 清除模版缓存

	$link[0]['text'] = '活动链接列表';
	$link[0]['href'] = 'act_url.php?act=list';

	sys_msg('编辑' . "&nbsp;" .$act_name . "&nbsp;" . '成功',0, $link);

}

elseif ($_REQUEST['act'] == 'remove')
{
	admin_priv('activity_url');

	$act_id = !empty($_GET['id']) ? intval($_GET['id']) : 0;
	$exc->drop($act_id);

	admin_log('', 'remove', '活动链接');

	$url = 'act_url.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

	wap_header("Location: $url\n");
	exit;
}

/* 获得文章列表 */
function get_act_url_list()
{
	$result = get_filter();
	if ($result === false)
	{
		$filter = array();
		$filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
		if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
		{
			$filter['keyword'] = json_str_iconv($filter['keyword']);
		}
		$filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'act_id' : trim($_REQUEST['sort_by']);
		$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

		$where = '';
		if (!empty($filter['keyword']))
		{
			$where = " AND act_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%'";
		}

		/* 总数 */
		$sql = "SELECT COUNT(*) FROM ".TABLEPRE."act_url";
		$filter['record_count'] = $GLOBALS['db']->getOne($sql);

		$filter = page_and_size($filter);

		/* 获取数据 */
		$sql = "SELECT act_id, act_name, url, start_time, stop_time, add_time FROM ".TABLEPRE."act_url ". $where;
		$filter['keyword'] = stripslashes($filter['keyword']);
		set_filter($filter, $sql);

	}
	else
	{
		$sql    = $result['sql'];
		$filter = $result['filter'];
	}
	$arr = array();
	$res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

	while ($rows = $GLOBALS['db']->fetchRow($res))
	{
		$rows['start_time'] = date('Y-m-d H:i:s', $rows['start_time']);
		$rows['stop_time'] = date('Y-m-d H:i:s', $rows['stop_time']);

		$arr[] = $rows;
	}
	return array('arr' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}